package com.jz.pay.study;

import com.jz.pay.bo.PaymentBO;
import com.jz.pay.controller.PaymentController;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

// InitializingBean, BeanFactoryAware, BeanNameAware
@Component
public class InstantiationAwareBeanPostProcessorImpl implements InstantiationAwareBeanPostProcessor /*, BeanFactoryAware*/ {
    /**
     * 返回null: 实例化该bean
     * 不返回null：不实例化该bean see postProcessBeforeInstantiation()
     *           此时通过 postProcessAfterInitialization()，自行实例化
     * @param beanClass
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if (beanClass.isAssignableFrom(PaymentController.class)){
            System.out.println("执行 InstantiationAwareBeanPostProcessorImpl.postProcessBeforeInstantiation....before实例化");
//            System.out.println("执行 beanName...."+beanName);
            return beanClass;
        }
        return null;
    }
//postProcessBeforeInstantiation，postProcessAfterInitialization
    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
//        System.out.println("beanName:"+beanName);
        if (bean instanceof PaymentController || beanName.contains("paymentController")){
            System.out.println("执行 InstantiationAwareBeanPostProcessorImpl.postProcessAfterInstantiation....after实例化");
//            System.out.println("执行 beanName...."+beanName);

        }
        return true;
    }

    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        if (bean instanceof PaymentController){
            System.out.println("执行 InstantiationAwareBeanPostProcessorImpl.postProcessProperties....");
//            System.out.println("执行 beanName...."+beanName);

        }
        return null;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof PaymentController){
            System.out.println("执行 InstantiationAwareBeanPostProcessorImpl.postProcessBeforeInitialization....初始化before");
//            System.out.println("执行 beanName...."+beanName);

        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof Class){
            Class clazz= (Class) bean;
            if (clazz.isAssignableFrom(PaymentController.class)){
                System.out.println("执行 InstantiationAwareBeanPostProcessorImpl.postProcessAfterInitialization....初始化after1");
//            System.out.println("执行 beanName...."+beanName);
                try {
                    return clazz.newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }else if(bean instanceof PaymentController){
            System.out.println("执行 InstantiationAwareBeanPostProcessorImpl.postProcessAfterInitialization....初始化after2");
        }

        return bean;
    }
}
